import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_1911/dao/travel_tab_dao.dart';
import 'package:flutter_1911/model/traveltab_model.dart';
import 'package:flutter_1911/pages/travel_tab_page.dart';
import 'package:flutter_1911/pic/instagram_filter_selection.dart';
import 'package:flutter_1911/until/nav_until.dart';
import 'package:flutter_1911/video_clip/clip.dart';
import 'package:image_picker/image_picker.dart';

class TravelPage extends StatefulWidget {
  const TravelPage({Key? key}) : super(key: key);

  @override
  _TravelPageState createState() => _TravelPageState();
}
//剪辑视频页面
class _TravelPageState extends State<TravelPage> with SingleTickerProviderStateMixin{
  TabController? _controller;
  List<Travel>? tabs = [];
  TravelTabModel? travelTabModel;
  late File _image;
  final picker = ImagePicker();


  @override
  void initState() {
    TravelTabDao.fetch().then((value){
      setState(() {
        tabs = value.tabs;
        travelTabModel = value;
        _controller = TabController(length: tabs!.length, vsync: this);
      });
    }).catchError((e){
      print(e);
    });
    super.initState();
  }
  @override
  void dispose() {
    _controller?.dispose();
    super.dispose();
  }
  //获取视频
  Future getVideo() async {
    final pickedFile = await picker.getVideo(source: ImageSource.camera);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
        print(_image.absolute);
      } else {
        print('No video selected.');
      }
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            color: Colors.white,
            padding: const EdgeInsets.only(top: 30),
            child: _controller == null?Text('加载中..'):TabBar(
              controller: _controller,
              isScrollable: true,
              labelColor: Colors.black,
              labelPadding: const EdgeInsets.fromLTRB(20, 0, 10, 5),
              indicator: const UnderlineTabIndicator(
                borderSide: BorderSide(color: Colors.teal, width: 3),
                insets: EdgeInsets.only(bottom: 10)
              ),
              tabs: tabs!.map((e) => Tab(text: e.labelName,)).toList(),
              ),
            ),
          Flexible(
              child:TabBarView(
              controller: _controller,
              children: tabs!.map((e) {
                return TravelTabPage(travelUrl: travelTabModel!.url, groupChannelCode: e.groupChannelCode);
              }).toList()
          ))
        ],
      ),
      //旅拍页面右下角的+号按钮
      floatingActionButton: FloatingActionButton(
        child: const Icon(Icons.add,color: Colors.amber,),
        backgroundColor: Colors.purple,
        onPressed: () {
          showModalBottomSheet(
              context: context,
              backgroundColor: Colors.red,
              shape: const RoundedRectangleBorder(
                borderRadius: BorderRadius.only(
                  //设置+号的左右间距 来保持居中
                    topLeft: Radius.circular(20.0),
                    topRight: Radius.circular(20.0)),
              ),
                  //点击后出现提示框 来显示要干什么
              builder: (BuildContext context) {
                return Container(
                  height: 300,
                  child: Column(
                    children: [
                      Container(
                        decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 1.0, color: Colors.black12)),
                        ),
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () {
                            Navigator.pop(context);
                            NavigatorUtil.push(
                                context, ExampleInstagramFilterSelection());
                          },
                          child: const Center(
                            child: Text(
                              '为照片添加滤镜',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold,color: Colors.green),
                            ),
                          ),
                        ),
                      ),

                      Container(
                        decoration: const BoxDecoration(
                          border: Border(
                              bottom: BorderSide(
                                  width: 1.0, color: Colors.black12)),
                        ),
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () {
                            getVideo();
                          },
                          child: const Center(
                            child: Text(
                              '拍摄视频',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold,color: Colors.green),
                            ),
                          ),
                        ),
                      ),

                      Container(
                        height: 80,
                        width: 750,
                        child: InkWell(
                          onTap: () async {
                            FilePickerResult? result =
                            await FilePicker.platform.pickFiles(
                              type: FileType.video,
                              allowCompression: false,
                            );
                            if (result != null) {
                              File file = File(result.files.single.path!);
                              Navigator.of(context).push(
                                MaterialPageRoute(builder: (context) {
                                  return Clip(file);
                                }),
                              );
                            }
                          },
                          child: const Center(
                            child: Text(
                              '剪辑视频',
                              style: TextStyle(
                                  fontSize: 20, fontWeight: FontWeight.bold,color: Colors.green),
                            ),
                          ),
                        ),
                      )
                    ],
                  ),
                );
              });
        },
      ),
    );
  }
}


